package com.wing.bill.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.bill.dao.WarehouseBillDetailDao;
import com.wing.bill.model.entity.WarehouseBillDetail;
import com.wing.bill.model.enums.BillStatusEnum;
import com.wing.bill.model.response.WarehouseBillDetailVO;
import com.wing.bill.model.response.WarehouseBillVO;
import com.wing.common.utils.DateUtils;
import com.wing.common.utils.PageResult;
import com.wing.product.model.response.WarehouseProductVO;
import com.wing.product.model.response.WarehouseStorageOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.bill.model.entity.WarehouseBill;
import com.wing.bill.dao.WarehouseBillDao;

@Service
public class WarehouseBillServiceImpl extends ServiceImpl<WarehouseBillDao, WarehouseBill> implements WarehouseBillService{

    @Autowired
    WarehouseBillDetailService warehouseBillDetailService;
    @Autowired
    WarehouseBillDetailDao warehouseBillDetailDao;

    @Override
    public Page<WarehouseBillVO> pageList4app(Page pg,  Map<String, Object> paramMap) {
        List<WarehouseBillVO> list = baseMapper.list4app(pg, paramMap);
        pg.setRecords(list);
        return pg;
    }

    // 判断用户账单是否存在
    @Override
    public WarehouseBill isExistBill(Long customerId){
        return baseMapper.selectOne(new LambdaQueryWrapper<WarehouseBill>().eq(WarehouseBill::getCustomerId, customerId).eq(WarehouseBill::getBillPeriod, DateUtils.getYearMonth()));
    }
    // 新增本月账单
    @Override
    public WarehouseBill createBill(Long customerId){
        WarehouseBill warehouseBill = new WarehouseBill();
        warehouseBill.setCustomerId(customerId);
        warehouseBill.setBillStatus(BillStatusEnum.UNPAID.getCode());
        warehouseBill.setBillPeriod(DateUtils.getYearMonth());
        baseMapper.insert(warehouseBill);
        return warehouseBill;
    }

    @Override
    public WarehouseBill settlement(Long id , Double amount ,String billVoucherUrl) {
        WarehouseBill bill = baseMapper.selectById(id);
        bill.setBillStatus(1);
        bill.setBillPrice(amount);
        bill.setBillVoucherUrl(billVoucherUrl);
        baseMapper.updateById(bill);
        warehouseBillDetailService.updateBillDetailByBillId(bill.id);
        return bill;
    }

    @Override
    public WarehouseBillVO selectBill(String id){
        WarehouseBillVO warehouseBillVO = this.baseMapper.selectBill(id);
        LambdaQueryWrapper<WarehouseBillDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WarehouseBillDetail::getWarehouseBillId,id)
                .eq(WarehouseBillDetail::getIsDeleted,0);
//        List<WarehouseBillDetail> warehouseBillDetails = warehouseBillDetailDao.selectBillDetailList(Long.parseLong(id));
        List<WarehouseBillDetailVO> warehouseBillDetails = warehouseBillDetailDao.selectBillDetailList(Long.parseLong(id));
        warehouseBillVO.setWarehouseBillDetailVOList(warehouseBillDetails);
        return warehouseBillVO;
    }
}
